package Homework;

import java.util.ArrayList;
import java.util.Iterator;

//测试类 用ArrayList
public class NewsTest {
	public static void main(String[] args) {
		//创建ArrayList集合，newTitle对象
		ArrayList<NewsTitle> arrayList = new ArrayList<>();
		
		//向ArrayList集合中添加对象
		arrayList.add(new NewsTitle(1, "金融危机", "Tom"));
		arrayList.add(new NewsTitle(2, "美国大选", "Jery"));
		arrayList.add(new NewsTitle(3, "苹果12上市", "Judy"));
		
		System.out.println("新闻标题总数："+arrayList.size());
		//逐条打印新闻内容
		//①用增强型的for输出
		for(NewsTitle i:arrayList) {
			System.out.println(i);
		}
		System.out.println("*******************************************");
		//②通过下标输出
		for(int i=0;i<arrayList.size();i++) {
			System.out.println(arrayList.get(i));
		}
		System.out.println("*******************************************");
		//③通过Iterator输出
		Iterator<NewsTitle> iterator=arrayList.iterator();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		System.out.println("*******************************************");
		//添加新闻头条
		arrayList.add(0,new NewsTitle(111, "美国疫情", "Andy"));
		System.out.println(arrayList);
		System.out.println("*******************************************");
		//获取头条新闻
		NewsTitle first=arrayList.get(0);
		System.out.println("头条新闻：\n"+first);
		//获取最末条新闻
		NewsTitle last=arrayList.get(arrayList.size()-1);
		System.out.println("最后一条新闻：\n"+last);
		System.out.println("*******************************************");
		//删除第二条新闻标题
		arrayList.remove(1);
		System.out.println(arrayList);
	}
}
